
package com.task.base;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

public class MyTaskManagerActivity extends TabActivity implements OnTabChangeListener {
    private TabHost m_tabHost;

    private TextView tabTextProcess;

    private TextView tabTextMemory;

    private TextView tabTextApplication;

    private NotificationManager nm;

    private Notification notification;

    private int notificationId = 597777;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.ThemeLight);
        setContentView(R.layout.tab);
        // getTabHost返回的TabHost用于装载tabs
        m_tabHost = getTabHost();
        Intent intent;
        TabSpec spec;

        intent = new Intent();
        intent.setClass(this, ProcessActivity.class);
        spec = getTabSpec("process", R.layout.tabhost_indicator_process, intent);
        m_tabHost.addTab(spec);

        intent = new Intent();
        intent.setClass(this, ApplicationActivity.class);
        spec = getTabSpec("appplication", R.layout.tabhost_indicator_application, intent);
        m_tabHost.addTab(spec);

        intent = new Intent();
        intent.setClass(this, MemoryActivity.class);
        spec = getTabSpec("memory", R.layout.tabhost_indicator_memeory, intent);
        m_tabHost.addTab(spec);

        tabTextProcess = (TextView) m_tabHost.getTabWidget().findViewById(R.id.tabTextProcess);
        tabTextMemory = (TextView) m_tabHost.getTabWidget().findViewById(R.id.tabTextMemory);
        tabTextApplication = (TextView) m_tabHost.getTabWidget().findViewById(
                R.id.tabTextApplication);
        tabTextProcess.setTextColor(Color.BLACK);
        tabTextApplication.setTextColor(Color.GRAY);
        tabTextMemory.setTextColor(Color.GRAY);
        m_tabHost.setOnTabChangedListener(this);

        nm = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
        notification = new Notification(R.drawable.icon, getString(R.string.app_name),
                System.currentTimeMillis());
        notification.contentView = new RemoteViews(getPackageName(), R.layout.notification);
        Intent openTaskIntent = new Intent(this, MyTaskManagerActivity.class);
        openTaskIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, openTaskIntent, 0);
        notification.contentIntent = pendingIntent;
        notification.flags = Notification.FLAG_ONGOING_EVENT;

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        boolean showNotification = prefs.getBoolean("system", true);
        // 是否显示状态栏图标
        if (showNotification) {
            nm.notify(notificationId, notification);
        }
        SharedPreferences sharedPreferences = this.getSharedPreferences("settings",
                Context.MODE_PRIVATE);
        boolean whertherAddShortcut = sharedPreferences.getBoolean("whertherAddShortcut", true);
        if (whertherAddShortcut) {
            Util.addShortcut(this);
            Editor editor = sharedPreferences.edit();
            editor.putBoolean("whertherAddShortcut", false);
            editor.commit();
        }
    }

    public TabSpec getTabSpec(String name, int layoutId, Intent content) {

        LayoutInflater mInflater = getLayoutInflater();
        TabSpec spec = m_tabHost.newTabSpec(name);
        View view = mInflater.inflate(layoutId, null);
        spec.setIndicator(view);
        spec.setContent(content);
        return spec;
    }

    @Override
    public void onDestroy() {
        if (nm != null) {
            nm.cancel(notificationId);
        }
        notification = null;
        nm = null;

        super.onDestroy();
    }

    @Override
    public void onTabChanged(String tabId) {

        if ("process".equals(tabId)) {
            tabTextProcess.setTextColor(Color.BLACK);
            tabTextApplication.setTextColor(Color.GRAY);
            tabTextMemory.setTextColor(Color.GRAY);
        } else if ("appplication".equals(tabId)) {
            tabTextProcess.setTextColor(Color.GRAY);
            tabTextApplication.setTextColor(Color.BLACK);
            tabTextMemory.setTextColor(Color.GRAY);
        } else if ("memory".equals(tabId)) {
            tabTextProcess.setTextColor(Color.GRAY);
            tabTextApplication.setTextColor(Color.GRAY);
            tabTextMemory.setTextColor(Color.BLACK);
        }

    }
}
